package com.ny.zmb.openapi.jh.service;

import com.alibaba.fastjson.JSONObject;
import com.mzlion.easyokhttp.HttpClient;
import com.ny.zmb.openapi.constant.JHConstant;
import com.ny.zmb.openapi.jh.JhEnum;
import com.ny.zmb.openapi.jh.UUIDUtil;
import com.ny.zmb.openapi.jh.from.CategoryVo;
import com.ny.zmb.openapi.jh.from.ProductSearchFrom;

import java.util.List;

/**
 * 商品管理
 */
public class JHProductService {

    /**
     * 获取商品列表
     * @param pageNo
     * @param prodId
     * @return
     */
    public static List<Integer> getProductList(Integer pageNo,Long prodId){
        JSONObject jsonObject = new JSONObject();
        if (null != pageNo){
            jsonObject.put("pageNo",pageNo);
        }
        if(null != prodId){
            jsonObject.put("prodId",prodId);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.product.list")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result = JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONObject("data").getJSONArray("row").toJavaList(Integer.class);
        }
        throw new RuntimeException(result.getString("msg"));
    }

    /**
     * 获取商品详情
     * @param prodId
     * @return
     */
    public static JSONObject getProductDetail(Integer prodId){
        JSONObject jsonObject = new JSONObject();
        if(null != prodId){
            jsonObject.put("prodId",prodId);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.product.detail")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result = JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONObject("data");
        }
        throw new RuntimeException(result.getString("msg"));
    }

    /**
     * 获取商品状态,价格
     * @param prodIds
     * @return
     */
    public static List<JSONObject> getProductPrice(Integer prodIds){
        JSONObject jsonObject = new JSONObject();
        if(null != prodIds){
            jsonObject.put("prodIds",prodIds);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.product.price")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result = JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONArray("data").toJavaList(JSONObject.class);
        }
        throw new RuntimeException(result.getString("msg"));
    }

    /**
     * 商品查询
     * @param from
     * @return
     */
    public static JSONObject getProductSearch(ProductSearchFrom from){
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.product.search")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",JSONObject.toJSONString(from))
                .execute().asBean(JSONObject.class);
        result = JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONObject("data");
        }
        throw new RuntimeException(result.getString("msg"));
    }
}
